﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SQLite;
using System.Data;

namespace Twitter_Data_Crawler
{
    class clsDbAccess
    {
        private string mcstrConnectinoString = "Data Source=" + System.Environment.CurrentDirectory + @"\data\db.s3db;Version=3;New=False;Compress=True;";

        private SQLiteConnection mobjConnection;
        private SQLiteDataAdapter mobjAdapter;
        private SQLiteTransaction mobjTrans;
        private SQLiteCommand mobjCommand;

        public clsDbAccess()
        {
            mobjConnection = new SQLiteConnection(mcstrConnectinoString);
            mobjCommand = mobjConnection.CreateCommand();
        }

        public bool fncOpen()
        {
            try
            {
                if (mobjConnection.State == ConnectionState.Open) mobjConnection.Close();
                mobjConnection.Open();
            }
            catch { return false; }
            return true;
        }

        public bool fncClose()
        {
            try
            {
                mobjConnection.Close();
            }
            catch { return false; }
            return true;
        }

        public bool fncBeginTransaction()
        {
            try
            {
                mobjTrans = mobjConnection.BeginTransaction();
            }
            catch { return false; }
            return true;
        }

        public bool fncCommit()
        {
            try
            {
                if (mobjTrans == null) return false;
                mobjTrans.Commit();
            }
            catch { return false; }
            return true;
        }

        public bool fncRollback()
        {
            try
            {
                if (mobjTrans == null) return false;
                mobjTrans.Rollback();
            }
            catch { return false; }
            return true;
        }

        public bool fncExcuteSQL(string strSQL, bool blnUseTrans = false)
        {
            try
            {
                if (mobjConnection.State == ConnectionState.Closed) fncOpen();
                mobjCommand.CommandText = strSQL;
                if (blnUseTrans) mobjCommand.Transaction = mobjTrans;
                int intResult = mobjCommand.ExecuteNonQuery();

                if (intResult <= 0) return false;
                return true;
            }
            catch (Exception ex)
            {
                //System.Windows.Forms.MessageBox.Show(ex.Message);
                return false;
            }
        }

        public DataTable fncReadData(string strSQL)
        {
            try
            {
                DataTable objData = new DataTable();

                if (mobjConnection.State == ConnectionState.Closed) fncOpen();

                mobjAdapter = new SQLiteDataAdapter(strSQL, mobjConnection);
                mobjAdapter.Fill(objData);

                if (objData == null) return null;
                if (objData.Rows.Count <= 0) return null;

                return objData;
            }
            catch { return null; }
        }

    }
}
